﻿using Assignment_3.Classes;
using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Assignment_3.Feature___Booking
{
    public partial class AddBooking : System.Web.UI.Page
    {
        bool requiredFields = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            calCheckInExTbx.StartDate = DateTime.Today;
            calCheckInExBtn.StartDate = DateTime.Today;

            calCheckOutExTbx.StartDate = DateTime.Today.AddDays(1);
            calCheckOutExBtn.StartDate = DateTime.Today.AddDays(1);

            calExpiryExTbx.StartDate = DateTime.Today;
            calExpiryExBtn.StartDate = DateTime.Today;

            pnlRoomTypeConfig.Controls.Clear();
            pnlBedTypeConfig.Controls.Clear();

            if (ddlRoom.SelectedIndex > 0)
            {
                rblRoomTypeD.Visible = false;
                ddlBedTypeD.Visible = false;

                for (int i = 1; i <= int.Parse(ddlRoom.SelectedValue); i++)
                {
                    Label lblRoomNo = new Label();
                    lblRoomNo.Font.Bold = true;
                    lblRoomNo.Text = "Room [" + i.ToString() + "]:" + "<p></p>";
                    pnlRoomTypeConfig.Controls.Add(lblRoomNo);

                    RadioButtonList rblRoomType = new RadioButtonList();
                    rblRoomType.ID = rblRoomType + i.ToString();

                    rblRoomType.Items.Add(new ListItem("Smoking", "Smoking"));
                    rblRoomType.Items.Add(new ListItem("Non-smoking", "Non-smoking"));
                    pnlRoomTypeConfig.Controls.Add(rblRoomType);

                    Label lblSpace = new Label();
                    lblSpace.Text = "<br />";
                    pnlRoomTypeConfig.Controls.Add(lblSpace);

                    Label lblSpaceTop = new Label();
                    lblSpaceTop.Text = "<p></p><br />";
                    pnlBedTypeConfig.Controls.Add(lblSpaceTop);

                    DropDownList ddlBedType = new DropDownList();
                    ddlBedType.ID = ddlBedType + i.ToString();

                    ddlBedType.Items.Add(new ListItem("King", "King"));
                    ddlBedType.Items.Add(new ListItem("Queen", "Queen"));
                    ddlBedType.Items.Add(new ListItem("Double", "Double"));
                    ddlBedType.Items.Add(new ListItem("Single", "Single"));
                    pnlBedTypeConfig.Controls.Add(ddlBedType);

                    Label lblSpaceBottom = new Label();
                    lblSpaceBottom.Text = "<br /><br /><br /><br />";
                    pnlBedTypeConfig.Controls.Add(lblSpaceBottom);
                }
                panelMsg.Controls.Clear();
            }
        }

        protected void rblPaymentType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblPaymentType.SelectedValue == "Credit Card")
            {
                lblCardholderName.Visible = true;
                tbxCardholder.Visible = true;
                lblCreditCardNo.Visible = true;
                tbxCreditCardNo.Visible = true;
                lblExpiryDate.Visible = true;
                tbxExpiryDate.Visible = true;
                imgBtnCal3.Visible = true;
            }
            else
            {
                lblCardholderName.Visible = false;
                tbxCardholder.Visible = false;
                lblCreditCardNo.Visible = false;
                tbxCreditCardNo.Visible = false;
                lblExpiryDate.Visible = false;
                tbxExpiryDate.Visible = false;
                imgBtnCal3.Visible = false;
            }
        }

        protected void ddlRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            rblRoomTypeD.Visible = false;
            ddlBedTypeD.Visible = false;

            pnlRoomTypeConfig.Controls.Clear();
            pnlBedTypeConfig.Controls.Clear();

            if (ddlRoom.SelectedIndex == 0)
            {
                rblRoomTypeD.Visible = true;
                ddlBedTypeD.Visible = true;
            }
            if (ddlRoom.SelectedIndex > 0)
            {
                for (int i = 1; i <= int.Parse(ddlRoom.SelectedValue); i++)
                {
                    Label lblRoomNo = new Label();
                    lblRoomNo.Font.Bold = true;
                    lblRoomNo.Text = "Room [" + i.ToString() + "]:" + "<p></p>";
                    pnlRoomTypeConfig.Controls.Add(lblRoomNo);

                    RadioButtonList rblRoomType = new RadioButtonList();
                    rblRoomType.ID = rblRoomType + i.ToString();

                    ListItem itemSmoking = new ListItem("Smoking", "Smoking");
                    ListItem itemNonSmoking = new ListItem("Non-smoking", "Non-smoking");
                    itemNonSmoking.Selected = true;

                    rblRoomType.Items.Add(itemSmoking);
                    rblRoomType.Items.Add(itemNonSmoking);

                    pnlRoomTypeConfig.Controls.Add(rblRoomType);

                    Label lblSpace = new Label();
                    lblSpace.Text = "<br />";
                    pnlRoomTypeConfig.Controls.Add(lblSpace);

                    Label lblSpaceTop = new Label();
                    lblSpaceTop.Text = "<p></p><br />";
                    pnlBedTypeConfig.Controls.Add(lblSpaceTop);

                    DropDownList ddlBedType = new DropDownList();
                    ddlBedType.ID = ddlBedType + i.ToString();

                    ddlBedType.Items.Add(new ListItem("King", "King"));
                    ddlBedType.Items.Add(new ListItem("Queen", "Queen"));
                    ddlBedType.Items.Add(new ListItem("Double", "Double"));
                    ddlBedType.Items.Add(new ListItem("Single", "Single"));
                    pnlBedTypeConfig.Controls.Add(ddlBedType);

                    Label lblSpaceBottom = new Label();
                    lblSpaceBottom.Text = "<br /><br /><br /><br />";
                    pnlBedTypeConfig.Controls.Add(lblSpaceBottom);
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Page.Validate();

            //if (rblPaymentType.SelectedValue == "Cash")
            //{
            //    RequiredFieldValidator9.IsValid = true;
            //    RequiredFieldValidator10.IsValid = true;
            //    RequiredFieldValidator11.IsValid = true;
            //}

            if (Page.IsValid)
            {
                Guest g = new Guest();
                g.Name = tbxGuestName.Text;
                g.Phone = Convert.ToInt32(tbxPhoneNo.Text);
                g.Email = tbxEmail.Text;
                g.Address = tbxAddress.Text;
                g.PostalCode = Convert.ToInt32(tbxPostalCode.Text);
                g.Country = tbxCountry.Text;
                g.PaymentType = rblPaymentType.SelectedValue.ToString();
                int successGuest = DatabaseManager.CreateGuest(g);

                Booking b = new Booking();
                b.Check_In_Date = DateTime.Parse(tbxCheckIn.Text);
                b.Check_Out_Date = DateTime.Parse(tbxCheckOut.Text);
                b.No_Of_Rooms = Convert.ToInt32(ddlRoom.SelectedValue);
                b.No_Of_Adults = Convert.ToInt32(ddlAdults.SelectedValue);
                b.No_Of_Children = Convert.ToInt32(ddlChildren.SelectedValue);
                b.GuestID = DatabaseManager.RetrieveGuestID();
                b.StaffID = DatabaseManager.RetrieveStaffID();
                int successBooking = DatabaseManager.CreateBooking(b);

                Room r = new Room();
                r.Room_Type = rblRoomTypeD.SelectedValue.ToString();
                r.Bed_Type = ddlBedTypeD.SelectedValue.ToString();

                string status = "";
                if (rblBookingType.SelectedItem.Value == "Direct Check-in")
                {
                    status = "Occupied";
                }
                if (rblBookingType.SelectedItem.Value == "Advance Booking")
                {
                    status = "Reserved";
                }
                r.Status = status;
                r.BookingID = DatabaseManager.RetrieveBookingID();

                int ddlbedTypeID = 0;
                double kingRate = 550.00;
                double queenRate = 500.00;
                double doubleRate = 420.00;
                double singleRate = 310;

                if (ddlBedTypeD.SelectedValue.Equals("King"))
                {
                    r.Room_Rates = kingRate;
                }
                else if (ddlBedTypeD.SelectedValue.Equals("Queen"))
                {
                    r.Room_Rates = queenRate;
                }
                else if (ddlBedTypeD.SelectedValue.Equals("Double"))
                {
                    r.Room_Rates = doubleRate;
                }
                else if (ddlBedTypeD.SelectedValue.Equals("Single"))
                {
                    r.Room_Rates = singleRate;
                }

                Random rand = new Random();
                int roomNo = rand.Next(1000, 10000);

                for (int i = 0; i < b.No_Of_Rooms; i++)
                {
                    r.RoomNo = "R" + roomNo;
                    roomNo++;
                    int success = DatabaseManager.CreateRoom(r);
                }

                int roomID = DatabaseManager.RetrieveRoomIDByBID(r.BookingID) - 1;
                foreach (Control c in pnlBedTypeConfig.Controls)
                {
                    if (c is DropDownList)
                    {
                        ddlbedTypeID++;
                        roomID++;
                        DropDownList ddl = (DropDownList)c;
                        if (c.ClientID.Equals("ContentPlaceHolder1_System.Web.UI.WebControls.DropDownList" + ddlbedTypeID.ToString()))
                        {
                            Room r2 = new Room();
                            r2.Bed_Type = ddl.SelectedValue.ToString();
                            r2.RoomID = roomID;

                            if (ddl.SelectedValue.Equals("King"))
                            {
                                r2.Room_Rates = kingRate;
                            }
                            else if (ddl.SelectedValue.Equals("Queen"))
                            {
                                r2.Room_Rates = queenRate;
                            }
                            else if (ddl.SelectedValue.Equals("Double"))
                            {
                                r2.Room_Rates = doubleRate;
                            }
                            else if (ddl.SelectedValue.Equals("Single"))
                            {
                                r2.Room_Rates = singleRate;
                            }

                            DatabaseManager.UpdateBedType(r2);
                        }
                    }
                }

                int ddlRoomTypeID = 0;
                int roomID2 = DatabaseManager.RetrieveRoomIDByBID(r.BookingID) - 1;
                foreach (Control c in pnlRoomTypeConfig.Controls)
                {
                    if (c is RadioButtonList)
                    {
                        ddlRoomTypeID++;
                        roomID2++;
                        RadioButtonList rbl = (RadioButtonList)c;
                        if (c.ClientID.Equals("ContentPlaceHolder1_System.Web.UI.WebControls.RadioButtonList" + ddlRoomTypeID.ToString()))
                        {
                            Room r3 = new Room();
                            r3.Room_Type = rbl.SelectedValue.ToString();
                            r3.RoomID = roomID2;
                            DatabaseManager.UpdateRoomType(r3);
                        }
                    }
                }

                int successRoom = 0;
                if (DatabaseManager.CheckRoomRows(r.BookingID) == b.No_Of_Rooms)
                {
                    successRoom = 1;
                }

                int successPayment = 0;
                bool hasCreditCardPayment = false;
                if (rblPaymentType.SelectedValue.Equals("Credit Card", StringComparison.OrdinalIgnoreCase))
                {
                    Payment p = new Payment();
                    p.CardholderName = tbxCardholder.Text;
                    p.CreditCardNo = tbxCreditCardNo.Text;
                    p.ExpiryDate = DateTime.Parse(tbxExpiryDate.Text);

                    p.GuestID = DatabaseManager.RetrieveGuestID();
                    successPayment = DatabaseManager.CreatePayment(p);
                    hasCreditCardPayment = true;
                }

                if (hasCreditCardPayment)
                {
                    if (successGuest == 1 && successBooking == 1 && successRoom == 1 && successPayment == 1)
                    {
                        string jsPopup = "<script type='text/javascript'>" +
                                            "alert('Successfully Created Booking!');" +
                                            "window.location.href = 'RetrieveBookings.aspx';" +
                                            "</script>";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", jsPopup);
                    }
                }
                else
                {
                    if (successGuest == 1 && successBooking == 1 && successRoom == 1)
                    {
                        string jsPopup = "<script type='text/javascript'>" +
                                            "alert('Successfully Created Booking!');" +
                                            "window.location.href = 'RetrieveBookings.aspx';" +
                                            "</script>";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", jsPopup);
                    }
                }
            }

            else
            {
                string requiredValidationGroup = "bookingFields";
                foreach (BaseValidator validator in Page.Validators)
                {
                    if (validator.ValidationGroup == requiredValidationGroup)
                    {
                        bool valid = validator.IsValid;
                        if (valid)
                        {
                            validator.Validate();
                            valid = validator.IsValid;
                            validator.IsValid = true;
                        }
                        if (!valid)
                            requiredFields = true;
                    }
                }

                if (requiredFields)
                {
                    panelMsg.Controls.Clear();
                    Literal litMsg = new Literal();
                    litMsg.Text = "<h4 class='alert_error'>Error: You have not filled out some required information. Please review the page.</h4>";
                    panelMsg.Controls.Add(litMsg);
                }
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("RetrieveBookings.aspx");
        }
    }
}